package com.imoresoft.magic.util;

import java.util.List;

import org.apache.commons.lang.StringUtils;

import com.ambitor.grass.util.data.DataMap;
import com.ambitor.grass.util.data.DatasetList;
import com.ambitor.grass.util.data.IData;
import com.ambitor.grass.util.data.IDataset;

public class StringUtil {

	public static String trimSuffix(String str, String suffix) {
		return ((str.endsWith(suffix)) ? str.substring(0, str.length() - 1)
				: str);
	}

	public static String trimPrefix(String str, String prefix) {
		return ((str.startsWith(prefix)) ? str.substring(prefix.length()) : str);
	}

	@SuppressWarnings("unchecked")
	public static IData DataToUpperCase(IData data) {
		if (data == null)
			return data;
		IData new_data = new DataMap();
		String[] names = data.getNames();
		for (int i = 0; i < names.length; i++) {
			String name = names[i];
			Object value = data.get(name);
			new_data.put(name.toUpperCase(), value);
		}
		return new_data;
	}

	@SuppressWarnings("unchecked")
	public static IData DataToLowerCase(IData data) {
		if (data == null)
			return data;
		IData new_data = new DataMap();
		String[] names = data.getNames();
		for (int i = 0; i < names.length; i++) {
			String name = names[i];
			Object value = data.get(name);
			new_data.put(name.toLowerCase(), value);
		}
		return new_data;
	}

	@SuppressWarnings("unchecked")
	public static IDataset DatasetToUpperCase(IDataset dataset)
			throws Exception {
		if (dataset == null)
			return dataset;
		IDataset new_dataset = new DatasetList();
		for (int i = 0; i < dataset.count(); i++) {
			IData old_data = dataset.getData(i);
			if (old_data == null)
				continue;
			IData new_data = new DataMap();
			String[] names = old_data.getNames();
			for (int j = 0; j < names.length; j++) {
				String name = names[j];
				Object value = old_data.get(name);
				new_data.put(name.toUpperCase(), value);
			}
			new_dataset.add(new_data);
		}
		return new_dataset;
	}

	@SuppressWarnings("unchecked")
	public static IDataset DatasetToLowerCase(IDataset dataset)
			throws Exception {
		if (dataset == null)
			return dataset;
		IDataset new_dataset = new DatasetList();
		for (int i = 0; i < dataset.count(); i++) {
			IData old_data = dataset.getData(i);
			if (old_data == null)
				continue;
			String[] names = old_data.getNames();
			IData new_data = new DataMap();
			for (int j = 0; j < names.length; j++) {
				String name = names[j];
				Object value = old_data.get(name);
				new_data.put(name.toLowerCase(), value);
			}
			new_dataset.add(new_data);
		}
		return new_dataset;
	}

	public static String[] StringToUpperCase(String[] keys) {
		if (keys == null || keys.length == 0)
			return keys;

		String[] new_keys = new String[keys.length];
		for (int i = 0; i < keys.length; i++) {
			new_keys[i] = (keys[i].toUpperCase());
		}
		return new_keys;
	}

	public static String buildFilterStr(String searchKey, String[] keys,
			boolean precise) {
		if (searchKey == null || keys == null || searchKey.trim().equals("")
				|| keys.length == 0)
			return "";
		String[] searchKeys = searchKey.split("[\\s]+");
		String symbol = "~";
		if (precise)
			symbol = "=";

		StringBuilder filterStr = new StringBuilder();
		for (String str : searchKeys) {
			for (String key : keys)
				filterStr.append(key + symbol + str + ";");
		}
		filterStr.deleteCharAt(filterStr.lastIndexOf(";"));
		return filterStr.toString();
	}

	public static Integer[] strArrayToIntArray(String[] strs) {
		Integer[] ints = new Integer[strs.length];
		if (strs == null || strs.length == 0)
			return ints;
		else {
			for (int i = 0; i < strs.length; i++) {
				if (StringUtils.isNumeric(strs[i]) == false) {
					ints[i] = 0;
				}
				else {
					ints[i] = Integer
							.parseInt(StringUtils.trimToEmpty(strs[i]));
				}

			}
		}
		return ints;
	}

	public static int sum(Integer[] ints) {
		int sum = 0;
		if (ints == null || ints.length == 0)
			return sum;
		for (int i : ints) {
			sum += i;
		}
		return sum;
	}

	public static String buildDatasetTitles(IDataset dataset, String title,
			String seporator) throws Exception {
		StringBuilder str = new StringBuilder();
		if (dataset == null || dataset.count() <= 0
				|| StringUtils.isBlank(title))
			return str.toString();
		for (int i = 0; i < dataset.count(); i++) {
			IData data = dataset.getData(i);
			str.append(data.getString(title.toUpperCase()) + seporator);
		}
		return com.ambitor.grass.util.string.StringUtils.trimSuffix(str
				.toString(), seporator);
	}
	
	public static String buildKeywords(String keywords) {
		StringBuilder sb = new StringBuilder();
		if (StringUtils.isNotBlank(keywords)) {
			String[] keyWordArray = keywords.split("([\\s]+)|,");
			for (String kw : keyWordArray) {
				sb.append(kw);
				sb.append(",");
			}
		}
		if(sb.length()>0) return sb.substring(0, sb.length()-1);
		else return sb.toString();
	}
	
	public static String list2str(List<String> strList) {
		StringBuilder sb = new StringBuilder();
		for(String s : strList) {
			sb.append(s).append(",");
		}
		if(sb.length()==0) return "";
		return sb.substring(0, sb.length()-1);
	}
	
	public static String [] buildModuleTitles(String installPlaces) {
		String [] titles = installPlaces.split(",");
		for(int i=0; i<titles.length; i++) {
			titles[i] = "apro_" + titles[i];
		}
		return titles;
	}
}
